import {
	baseUrl
} from '@/common/api/config.js'
const http = (path, data = {}, method = 'POST') => {
	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('token')
		uni.request({
			url: baseUrl + '/member/' + path,
			data,
			header: {
				'content-type': 'application/json',
				Authorization: token.token ? 'Bearer ' + token.token : '',
				"Accept": "application/json, text/javascript, */*; q=0.01"
			},
			method,
			success(res) {
				if (res.statusCode == 200) {
					let data = res.data
					if (!data.code) {
						uni.$u.toast(data.msg)
						reject(res.data)
					} else if (data.code === 401) {
						uni.removeStorage({
							key: 'token'
						})
						let app_info = uni.getSystemInfoSync()
						console.log(app_info)
						// #ifdef MP-WEIXIN
						if (app_info.hostName == 'WeChat') { //小程序

						} else if (app_info.hostName == 'wxwork') { //企业微信
							uni.reLaunch({
								url: '/pages/work/not_login/not_login'
							})
						}
						// #endif
						reject(res.data)
					} else if (data.code == 403) {
						uni.reLaunch({
							url: '/pages/work/disabled_error/disabled_error'
						})
						reject(res.data)
					} else if (data.code == 1001) {
						uni.reLaunch({
							url: '/pages/shop/not_open/not_open'
						})
						reject(res.data)
					}else if(data.code == 1002){
						uni.showModal({
							content:'需要先绑定账户',
							showCancel:false,
							success() {
								uni.navigateTo({
									url:'/pages/work/user_setting/user_setting'
								})
							}
						})
						reject(res.data)
					}else if(data.code == 1003){
						uni.showModal({
							content:'您需要绑定与注册会员手机号一致',
							showCancel:false,
							success() {
								uni.navigateTo({
									url:'/pages/shop/user_setting/user_setting'
								})
							}
						})
						reject(res.data)
					}else if(data.code == 1999){
						uni.reLaunch({
							url:'/pages/work/index/index'
						})
						reject(res.data)
					}
					resolve(res.data)
				} else {
					reject(res.data)
				}
			},
			fail(err) {
				uni.showToast({
					title: '网络异常',
					icon: 'error'
				})
				reject(err)
			}
		})
	})
}
export default http